# -*- coding:utf-8 -*-

import smtplib, time
from os import path
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

class SendMail():
    def __init__(self):
        pass

    def send_mail(self, file_new):
        # 收件人邮箱的服务器
        SMTPserver = "smtp.exmail.qq.com"

        # 发送人的邮箱和密码
        sender = "yi.zhou@aihuishou.com"
        password = "这里输入公司邮箱密码"

        # 收件人的邮箱
        destination = "397034688@qq.com"
        destinations = destination.split(',')

        now = time.strftime('%Y-%m-%d')

        with open(file_new, "rb") as f:
            mail_body = f.read()

        # 邮件的标题Subject、发信人From、收件人To
        msg = MIMEMultipart()
        body = MIMEText(mail_body, _subtype="html", _charset="utf-8")
        msg["Subject"] = "自动化测试报告" + now
        msg["From"] = sender
        msg["To"] = ",".join(destinations)
        msg.attach(body)

        # 附件
        # part = MIMEApplication(open(file_new, 'wb').read())
        # part.add_header('Content-Disposition', 'attachment', filename=msg["Subject"] + '.html')
        # msg.attach(part)

        # 连接邮箱
        mailserver = smtplib.SMTP(SMTPserver, 25)
        mailserver.login(sender, password)
        mailserver.sendmail(sender, destination, msg.as_string())
        mailserver.quit()
        print("邮件已发送")



